package com.hsnn.medstgmini.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hsnn.medstgmini.Constants;
import com.hsnn.medstgmini.sys.model.SysPrivilege;
import com.hsnn.medstgmini.sys.model.SysRole;
import com.hsnn.medstgmini.sys.model.SysUser;
import com.hsnn.medstgmini.sys.service.SysPrivilegeManager;
import com.hsnn.medstgmini.sys.service.SysRoleManager;
import com.hsnn.medstgmini.util.TreeUtil;

/**
 * @category index页面
 * @author CCL
 */
@Controller
public class IndexController {

	@Autowired
	private SysRoleManager roleManager;

	@Autowired
	private SysPrivilegeManager privilegeManager;

	@Value("#{sysProp['main.leixing.yiju']}")
	private String gist;

	@Value("#{sysProp['main.yewu.xianshi']}")
	private String show;
	
    @Value("#{sysProp['system.title']}")
    private String systemTitle;
    
    @Value("#{sysProp['system.host.unit']}")
    private String systemHostUnit;

	/**
	 * @category 查询菜单列表并跳转index页面
	 * @param session
	 * @param model
	 * @return
	 */
	@RequestMapping("/index")
	public String index(HttpSession session, Model model) {
		SysUser user = (SysUser) session.getAttribute(Constants.USERINFO);

		boolean showBusiness = true;

		// 判断主帐号是否显示业务菜单
		if (user.getMain() == 1) {
			if ("0".equals(gist) && "1".equals(show)) {
				showBusiness = true;
			} else if ("1".equals(gist) && "1".equals(user.getBussinessShow())) {
				showBusiness = true;
			} else {
				showBusiness = false;
			}
		}

		// 查询用户角色列表
		List<SysRole> roleList = roleManager.getRoleListByUser(user);
		user.setRoleList(roleList);

		// 查询用户权限列表
		List<SysPrivilege> list = privilegeManager.getPrivilegeListByUser(user);
		user.setPrivilegeList(list);

		List<String> btnUrlList = new ArrayList<String>();
		// 权限集合存入session
		if (list != null && !list.isEmpty()) {
			for (SysPrivilege p : list) {
				if (!showBusiness && "1".equals(p.getClassify())) {
					continue;
				}
				if (p.getType() == 2 || p.getType() == 3) {
					if (StringUtils.isNotBlank(p.getUrl())) {
						String[] urls = p.getUrl().split(";");
						if (urls.length > 1) {
							for (int i = 0; i < urls.length; i++) {
								btnUrlList.add(urls[i]);
							}
						}
						btnUrlList.add(p.getUrl());
					}
				}
			}
		}
		session.setAttribute(Constants.USER_PRIVILELE, btnUrlList);

		// 菜单树
		model.addAttribute("list", TreeUtil.toMenuTree(list));
		model.addAttribute("user", user);

		model.addAttribute("systemHostUnit", systemHostUnit);
		model.addAttribute("systemTitle", systemTitle);
		return "/sysindex";
	}

}
